package cn.jtfadmin.base.lang.common.utils;

import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;
import java.util.Locale;
import java.util.Objects;

/**
 * 请求工具
 * @author jtf
 * @since 0.0.1
 */
public class RequestUtils {

    /**
     * 判断是否是ajax
     *
     * @param request
     * @return
     */
    public static boolean isAjax(HttpServletRequest request) {
        return Objects.equals(request.getHeader("X-Requested-With"), "XMLHttpRequest");
    }


    /**
     * 默认中文
     */
    private static Locale defaultLocale = Locale.CHINA;


    public static Locale getDefaultLocale() {
        return defaultLocale;
    }

    public static void setDefaultLocale(Locale defaultLocale) {
        RequestUtils.defaultLocale = defaultLocale;
    }

    /**
     * 获取请求语言
     *
     * @param request
     * @return
     */
    public static Locale getRequestLocale(HttpServletRequest request) {
        return request.getLocale();
    }

    /**
     * 从当前线程中获取request 获取Locale
     * 获取请求语言, 如果没有request 则返回默认local
     *
     * @return
     */
    public static Locale getRequestLocale() {
        HttpServletRequest request = getRequest();
        if (Objects.isNull(request)) {
            return getDefaultLocale();
        }
        return getRequestLocale(request);
    }

    /**
     * 获取当前线程的request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.nonNull(requestAttributes) && requestAttributes instanceof ServletRequestAttributes) {
            return ServletRequestAttributes.class.cast(requestAttributes).getRequest();
        }
        return null;
    }


    /**
     * 获取当前线程的response
     *
     * @return
     */
    public static HttpServletResponse getResponse() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (Objects.nonNull(requestAttributes) && requestAttributes instanceof ServletRequestAttributes) {
            return ServletRequestAttributes.class.cast(requestAttributes).getResponse();
        }
        return null;
    }

    /**
     * 获取地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        if (Objects.isNull(request)) {
            return null;
        }
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String[] split = ip.split("\\s+");
        for (String s : split) {
            if (StringUtils.hasText(s)) {
                return s.replaceAll(",", "");
            }
        }
        return ip.replaceAll(",", "");

    }

    /**
     * 获取请求的base url
     *
     * @param request
     * @return
     */
    public static String getRequestBasePath(HttpServletRequest request) {
        String path = request.getContextPath();
        String scheme = request.getScheme();
        int port = request.getServerPort();
        if ((scheme.equalsIgnoreCase("http") && port == 80)
                || (scheme.equalsIgnoreCase("https") && port == 443)) {
            return scheme + "://" + request.getServerName() + path;
        }
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
        return basePath;
    }



}
